<?php

namespace addons\prl\controller;
use addons\prl\model\Channels as ChannelsModel;
use addons\prl\model\Articles as ArticlesModel;
use addons\prl\model\Views as ViewsModel;
use think\Db;
class Info extends Base
{
	protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    protected $row = '';

    protected $articleModel = '';

    public function _initialize()
    {
        $this->articleModel = new ArticlesModel;
        $id = $this->request->param('id');
        $this->row = $this->articleModel->where('id', $id)->find();//增加浏览次数并获取到信息
        $this->row->views = $this->row->views + 1;
        $this->row->save();
        $this->current_channel = (new ChannelsModel)->channel($this->row->channels_id)->channelorder()->find();
        if(empty($this->current_channel) || empty($this->row))
            return $this->error('栏目或文章未找到！');

        $this->view_type = 3;
        parent::_initialize();
    }

    //详情页
    public function index()
    {
        //判断是否绑定模型
        if($this->current_channel->models !== null){
            //查询栏目是否绑定模型
            $moreInfo = Db::table($this->current_channel->models->table)->where('id', $this->row->id)->find();
            $this->row['more_info'] = $moreInfo;
        }
        //查找文章的上一篇和下一篇
        // 上一篇
        $pre = $this->articleModel->pre($this->current_channel->id, $this->row->id)->find();
        // 下一篇
        $next = $this->articleModel->next($this->current_channel->id, $this->row->id)->find();

        $this->assign('article_row', $this->row);
        $this->assign('pre', $pre);
        $this->assign('next', $next);
        $viewName = ViewsModel::where('id', $this->current_channel->info_view_id)->value('tpl');
        return $this->show($viewName);
    }
}